Tack.Scene = new Class({
	initialize: function (name) {
		this.name = name || 'Untitled';

		this.events = {
			entering: new Tack.ActionBlock(this),
			command: new Tack.ActionBlock(this)
		};
	},

	changeSceneReferences: function (oldName, newName) {
		$each(this.events, function (block) { block.changeSceneReferences(oldName, newName); });
	},

	compile: function () {
		var scene = { name: this.name, events: {} };

		$each(this.events, function (block, event) {
			scene.events[event] = block.compile();
		});

		return scene;
	},

	rename: function (newName) {
		if (this.name == newName) return;
		
		if (this.game) {
			if (this.game.scenes.has(newName))
				throw 'A scene with that name already exists.';
			
			this.game.changeSceneReferences(this.name, newName);
		}

		this.name = newName;
	},

	setGame: function (game) {
		if (!game instanceof Tack.Game)
			throw '[game] must be an instance of Tack.Game.';

		if (this.game == game) return;

		this.game = game;
	},

	triggerEvent: function (player, event, eventArgs) {
		if (!this.events[event])
			throw 'Unrecognized event type.';

		this.events[event].execute(player, eventArgs);
	}
});

Tack.Scene.decompile = function (obj) {
	var scene = new Tack.Scene(obj.name);

	for (var e in scene.events) {
		scene.events[e] = Tack.ActionBlock.decompile(obj.events[e], scene);
	}

	return scene;
};
